﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows;
namespace AutoUpgradeManager
{

    internal class FileBindingInfo : DependencyObject
    {

        public static DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(FileBindingInfo));
        public static DependencyProperty SizeProperty = DependencyProperty.Register("Size", typeof(long), typeof(FileBindingInfo));
        public static DependencyProperty PathProperty = DependencyProperty.Register("Path", typeof(string), typeof(FileBindingInfo));
        public static DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(string), typeof(FileBindingInfo));

        public string Name
        {
            get
            {
                return (string)this.Dispatcher.Invoke(new Func<string>(() => (string)this.GetValue(NameProperty)));
            }
            set
            {
                this.SetValue(NameProperty, value);
            }
        }

        public long Size
        {
            get
            {
                return (long)this.GetValue(SizeProperty);
            }
            set
            {
                this.SetValue(SizeProperty, value);
            }
        }

        public string Path
        {
            get
            {
                return (string)this.Dispatcher.Invoke(new Func<string>(() => (string)this.GetValue(PathProperty)));
            }
            set
            {
                this.SetValue(PathProperty, value);
            }
        }

        public string State
        {
            get
            {
                return (string)this.GetValue(StateProperty);
            }
            set
            {
                this.Dispatcher.BeginInvoke(new Action(()=>this.SetValue(StateProperty, value)));
            }
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
        }

        public FileBindingInfo(string name, long size, string path)
        {
            Name = name;
            Size = size;
            Path = path;
            State = "";
        }
    }

    internal class FileBindingCollection:ObservableCollection<FileBindingInfo>
    {

        public string ParentDirPath
        {
            get;
            private set;
        }

        public void RetrievalFolderPath(string FolderPath)
        {
            if (!System.IO.Directory.Exists(FolderPath)) return;

            this.Clear();

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(FolderPath);

            ParentDirPath = FolderPath;

            getFileList(dir);
            
        }

        private void getFileList(System.IO.DirectoryInfo dir)
        {
            try
            {
                foreach (System.IO.FileSystemInfo info in dir.GetFileSystemInfos())
                {
                    if (info is System.IO.FileInfo)
                    {
                        System.IO.FileInfo f = info as System.IO.FileInfo;
                        this.Add(new FileBindingInfo(f.Name, f.Length, f.DirectoryName));
                    }
                    else
                    {
                        System.IO.DirectoryInfo d = info as System.IO.DirectoryInfo;

                        if (d == null) continue;

                        getFileList(d);
                    }
                }
            }
            catch { }
        }

    }
}
